package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.MemberDto;
import edu.suda.xsap.entity.Member;
import edu.suda.xsap.entity.vo.IdVo;
import edu.suda.xsap.entity.vo.MemberCardVo;
import edu.suda.xsap.entity.vo.MemberVo;
import edu.suda.xsap.entity.vo.Page2;
import edu.suda.xsap.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user-management")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;
    /** 使用axios获取后端会员表总条目
axios.get('/api/user-management/get-total-records')*/
    @GetMapping("/get-total-records")
    public Result getTotalRecords() {
        try {
            Integer count = memberService.findCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /* 分页查询，前端将待跳转页面page与每页显示的行数pageSize传递给后端
axios.get('/api/user-management/members-page-change**/
    @PostMapping("/members-page-change")
    public Result getMembersPageChange(@RequestBody Page2 page2) {
        try {
            System.out.println(page2);
            List<MemberVo> memberVos = memberService.findByLimit(page2.getPageSize(), (page2.getPage() - 1) * page2.getPageSize());
            System.out.println(memberVos);
            return Result.success(memberVos);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /** axios.post('/api/user-management/edit-user', editUser.value).tconst editUser =
     })**/
    @PostMapping("/edit-user")
    public Result editUser(@RequestBody Member member) {
        try {
            System.out.println(member);
            member.setLastModifyTime(LocalDateTime.now());
            memberService.update(member);
            return Result.success();
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    /** axios.post('/api/user-management/view-user', */
    @PostMapping("/view-user")
    public Result viewUser(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            MemberDto memberDto = memberService.findById(id);
            System.out.println(memberDto);
            return Result.success(memberDto);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /**axios.post('/api/user-management/del-user', {"id": id})**/
    @PostMapping("/del-user")
    public Result delUser(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            memberService.delete(id);
            return Result.success();
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    /***axios.post('/api/user-management/save-user', form.value**/
    @PostMapping("/save-user")
    public Result saveUser(@RequestBody Member member) {
        try {
            System.out.println(member);
            member.setCreateTime(LocalDateTime.now());
            member.setLastModifyTime(LocalDateTime.now());
            IdVo idVo =memberService.save(member);
            return Result.success(idVo);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    /**axios.post('/api/user-management/get-user-cards', {"id": id})**/
    @PostMapping("/get-user-cards")
    public Result getUserCards(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            List<MemberCardVo> s= memberService.findCardByMemberId(id);
            System.out.println(s);
            return Result.success(s);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    /**user-management/get-user-mes**/
    @GetMapping("/get-user-mes")
    public Result getUserMes() {
        try {
            List<MemberDto> memberDtos = memberService.findAll();
            return Result.success(memberDtos);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

}
